package managedbean;

import entity.LineItem;
import entity.Product;
import javax.faces.event.ActionEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import session.stateless.PurchaseOrderBeanLocal;

@ManagedBean(name="addPOWithoutSQ")
@ViewScoped

public class AddPOWoSQManagerBean implements Serializable {
    
    public AddPOWoSQManagerBean () {
    }
    
    @EJB
    private PurchaseOrderBeanLocal purchaseOrderBeanLocal;
    
    private Long purchaseOrderId;
    private Long customerId;
    private Long saleQuotationId;
    private List<LineItem> lineItems;
    private List<Integer> quantities;

    private String statusMessage;

    public void initView(PhaseEvent event)
    {
        try
        {
            if(lineItems == null)
            {
                List<Product> products = purchaseOrderBeanLocal.getProduct();
                lineItems = new ArrayList<LineItem>();

                for(Product product:products)
                {
                    LineItem lineItem = new LineItem();
                    lineItem.setProduct(product);

                    lineItems.add(lineItem);
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public Long getPurchaseOrderId() {
        return purchaseOrderId;
    }

    public void setPurchaseOrderId(Long purchaseOrderId) {
        this.purchaseOrderId = purchaseOrderId;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public Long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Long customerId) {
        this.customerId = customerId;
    }

    public List<LineItem> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }

    public PurchaseOrderBeanLocal getPurchaseOrderBeanLocal() {
        return purchaseOrderBeanLocal;
    }

    public void setPurchaseOrderBeanLocal(PurchaseOrderBeanLocal purchaseOrderBeanLocal) {
        this.purchaseOrderBeanLocal = purchaseOrderBeanLocal;
    }

    public List<Integer> getQuantities() {
        return quantities;
    }

    public void setQuantities(List<Integer> quantities) {
        this.quantities = quantities;
    }

    public Long getSaleQuotationId() {
        return saleQuotationId;
    }

    public void setSaleQuotationId(Long saleQuotationId) {
        this.saleQuotationId = saleQuotationId;
    }

    public void saveNewPurchaseOrder(ActionEvent event)
    {
        Long temp_customerId = Long.valueOf(customerId);
        List<LineItem> temp_lineItems = lineItems;
               
        try
        {              
            purchaseOrderId = purchaseOrderBeanLocal.createPOWithoutSQ(temp_customerId, temp_lineItems);
            statusMessage = "New Purchase Order Saved Successfully.";
            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Add New Purchase Order Result: " + statusMessage + " (New Purchase Order ID: " + purchaseOrderId + ")", ""));
        }
        catch(Exception ex) 
        { 
            ex.printStackTrace(); 
        } 
        
    }
}
